﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Threading;
using DevExpress.Spreadsheet;
using DevExpress.XtraBars.Docking2010.Views.WindowsUI;
using DevExpress.XtraReports.UI;
using DevExpress.XtraSpreadsheet;
using EduFormManager.Forms.UserControls.QueryControl;
using EduFormManager.Models;
using EduFormManager.Models.Repo;
using DocumentFormat = DevExpress.Spreadsheet.DocumentFormat;

namespace EduFormManager.Forms.Reports
{
    public static class ReportFactory
    {
        public static Task<XtraReport> CreateQueryReportAsync(QuerySummaryModel model)
        {
            return Task.Run(() => CreateQueryReport(model));
        }

        public static XtraReport CreateQueryReport(QuerySummaryModel model)
        {
            IEnumerable<EduOrg> edus = model.EduList;
            IEnumerable<Query> queries = model.QueryList;
            int year = model.Year;
            Allform form = model.Form;
            QueryReportDataCollection reportDataList = new QueryReportDataCollection();
            using (SpreadsheetControl spreadsheetControl = new SpreadsheetControl() { AllowDrop = false })
            {
                foreach (EduOrg edu in edus)
                {
                    EduFormData formData = edu.FormData.FirstOrDefault(t => t.Form == form && t.Date.Year == year);
                    if (formData != null)
                    {
                        byte[] excelData = formData.File.Contents;
                        DocumentFormat format = formData.DocumentFormat.IsOpenXml
                            ? DocumentFormat.OpenXml
                            : DocumentFormat.Xls;
                        spreadsheetControl.LoadDocument(excelData, format);
                    }
                    foreach (Query query in queries)
                    {
                        string stringValue = "";
                        try
                        {
                            string content = query.Content;
                            if (content.StartsWith("@"))
                            {
                                string propName = content.Substring(1);
                                PropertyInfo propInfo = typeof (EduOrg).GetProperty(propName);
                                stringValue = propInfo.GetValue(edu).ToString();
                            }
                            else
                            {
                                CellValue value = spreadsheetControl.Document.Range[content].Value;
                                stringValue = GetStringValueFromCell(value);
                            }
                        }
                        catch (Exception ex)
                        {
                            stringValue = "Не удалось";
                        }
                        reportDataList.Add(new QueryReportData(edu, query, stringValue));
                    }
                }
            }
            List<QueryReportDataGroupEdu> groupedList = (List<QueryReportDataGroupEdu>)reportDataList.GroupByEdu();
            //List<QueryReportDataGroupQuery> groupedList = (List<QueryReportDataGroupQuery>)reportDataList.GroupByQuery();
            QueryReport report = new QueryReport {ReportData = groupedList};
            return report;
        }

        private static string GetStringValueFromCell(CellValue value)
        {
            return value != null ? value.ToString() : "NAN";
        }
    }
}
